package com.thinkfit.jy.utils;

import android.content.Context;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.interfaces.datasets.ILineDataSet;
import com.thinkfit.jy.R;

import java.util.ArrayList;
import java.util.List;

public class LineChartUtils {

    static LineChartUtils lineChartUtils;

    List<Entry> entries = new ArrayList<Entry>();
    LineChart chart;
    Context mContext;

    public LineChartUtils(Context context){
        mContext = context;
    }
//    public static LineChartUtils getInstance(){
//        if(lineChartUtils == null){
//            lineChartUtils = new LineChartUtils();
//        }
//
//        return lineChartUtils;
//    }

    public void initView(LineChart chart){
        this.chart = chart;

        Description description = chart.getDescription();
        description.setEnabled(false);


        //x轴
        XAxis xAxis = chart.getXAxis();
        xAxis.setTextColor(mContext.getResources().getColor(com.common.lib.R.color.color_ff_ff_ff));
        xAxis.setDrawGridLines(false);//是否绘制网格线(横向的线)
        xAxis.setAxisMinimum(5);
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);//设置X所在的位置

        //Y轴右
        YAxis axisRight = chart.getAxisRight();
        axisRight.setEnabled(false);//是否启动
        //Y轴左
        YAxis axisLeft = chart.getAxisLeft();
        axisLeft.setDrawLimitLinesBehindData(false);
        axisLeft.setTextColor(mContext.getResources().getColor(com.common.lib.R.color.color_ff_ff_ff));
        axisLeft.setDrawGridLines(true);//是否绘制网格线
        axisLeft.setGridColor(mContext.getResources().getColor(com.common.lib.R.color.color_14_ff_ff_ff));//网格线颜色


        // 图例设置
        Legend legend = chart.getLegend();
        legend.setEnabled(false);
        legend.setForm(Legend.LegendForm.LINE);
//        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.CENTER);//图例水平居中
//        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.TOP);//图例在图表上方
//        legend.setOrientation(Legend.LegendOrientation.VERTICAL);//图例的方向为水平
//        legend.setDrawInside(false);//绘制在chart的外侧
//        legend.setDirection(Legend.LegendDirection.LEFT_TO_RIGHT);//图例中的文字方向
//        legend.setForm(Legend.LegendForm.EMPTY);//图例窗体的形状


        ArrayList<ILineDataSet> dataSets = new ArrayList<>();
        LineData lineData = new LineData(dataSets);
        lineData.setDrawValues(false);


        // 启用拖动
        chart.setDragEnabled(false);
        // 启用缩放
        chart.setScaleEnabled(false);
        // 启用触摸手势
        chart.setTouchEnabled(false);
        chart.setDescription(description);
        chart.setData(lineData); // 图表绑定数据值
        chart.invalidate(); // 刷新图表

    }

    public void invalidate(float valX,float valY){
        if(chart == null){
            return;
        }
        entries.add(new Entry(valX,valY));
        LineDataSet dataSet = new LineDataSet(entries, "");
        dataSet.setDrawValues(false);
        dataSet.setCircleColor(mContext.getResources().getColor(R.color.white));//设置圆点的颜色
        dataSet.setLineWidth(2f);//设置线条的宽度
        dataSet.setColor(mContext.getResources().getColor(com.common.lib.R.color.color_ef_c1_4e));//设置线的颜色
        dataSet.setCircleRadius(1.5f);//设置圆点的半径大小
        dataSet.setDrawCircleHole(false);//设置是否为空心圆
        dataSet.setDrawFilled(false);//是否在线条下方设置填充
        LineData lineData = new LineData(dataSet);
        chart.setData(lineData); // 图表绑定数据值
        chart.invalidate(); // 刷新图表
    }

    public void clearValues(){
        initView(chart);
        entries.clear();

    }

}
